import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import getuser from '../../../../atoms/getuser';
import { tbdetailsofachievements, tbtrem } from '../../../../atoms/db';

const logger = anylogger('pages/api/zllweapp/gradequery/s002.api');

export type Result = {
	ok: true;
	trem_name: Pick<ITbtrem, 'trem_name' | 'trem_id'>[]
} | {
	ok: false;
	message: string;
};

export type Message = {

}

/**
 * pages/api/zllweapp/gradequery/s002.api
 */
const handler = an48<Result>();

handler.post(async (req, res) => {
	try {
		logger.debug('msg body:', req.body);

		//**获取登录用户信息 */
		const user = await getuser(req);

		const student_id = '201942908581';

		const trem_id = await tbdetailsofachievements().distinct('trem_id').where({
			student_id: student_id
		});

		const trem_id1 = trem_id.map((it) => {
			return it.trem_id;
		});
		const trem_name = await tbtrem().select('trem_name', 'trem_id').whereIn(
			'trem_id', trem_id1
		);
		res.status(200).json({ ok: true, trem_name });
	} catch (error) {
		logger.trace(error);
		res.status(200).json({ ok: false, message: (error as Error).message });
	}
});

export const config = {} as PageConfig;

export default handler;
